<?php

require_once("../../connection.php");

$reaction = $_POST['reaction'];
$user = $_POST['user'];
$postid = $_POST['postid'];

// checking if the user wants to unmark
$check = mysql_query("SELECT * FROM site_response WHERE `postid`='$postid' AND `fbid`='$user'");
$prev_reaction = mysql_result($check, 0, 'rid');
$del_or_not = mysql_result($check, 0, 'del');

if(mysql_numrows($check) != 0 && ($prev_reaction == $reaction) && $del_or_not == 0)
{
	
	// user wants to unmark
	$del = mysql_query("UPDATE site_response SET `del`='1' WHERE `postid`='$postid' AND `fbid`='$user' AND `rid`='$reaction'");
	if(!$del) echo mysql_error(); else echo "del";
	
}
else if(mysql_numrows($check) != 0 && ($prev_reaction != $reaction) && $del_or_not == 0)
{
	//user wants to mark something different
	$change = mysql_query("UPDATE site_response SET `rid`='$reaction' WHERE `postid`='$postid' AND `fbid`='$user'");
	if(!$change) echo mysql_error(); else echo "change";
	
}
else if(mysql_numrows($check) != 0 && $del_or_not == 1)
{
	$mark = mysql_query("UPDATE site_response SET `del`='0', `rid`='$reaction' WHERE `fbid`='$user' AND `postid`='$postid'");
	if(!$mark) echo mysql_error(); else echo "success";
}
else if(mysql_numrows($check) == 0)
{
	// user is marking for the first time
	
	$mark = mysql_query("INSERT INTO site_response(`rid`,`fbid`,`postid`) VALUES('$reaction','$user','$postid')");
	if(!$mark) echo mysql_error(); else echo "success";
}





?>